package sap.gui;

import com.sap.mw.jco.JCO;
import javax.swing.JOptionPane;
/*
 * OptionManager.java
 *
 * Created on 10 de noviembre de 2007, 11:12 AM
 */

/**
 *
 * @author  Enrique Garcia
 */
public class LoginManager extends javax.swing.JPanel implements java.awt.event.ActionListener{
    
    /**
     * Creates new form OptionManager
     */
    public LoginManager(CManager cm) {
        initComponents();
        addListeners();
        this.cm=cm;
        connection.setText(JCo_path);
    }
    
    private void addListeners()
    {
     jButton5.addActionListener(this);
     jButton7.addActionListener(this);
     jButton8.addActionListener(this);
     
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        connection = new javax.swing.JTextField();
        client = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        system = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        language = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        user = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPanel1 = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(0, 0, 0));
        setLayout(null);

        jProgressBar1.setBackground(new java.awt.Color(0, 0, 0));
        add(jProgressBar1);
        jProgressBar1.setBounds(10, 460, 560, 20);

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Conecction Satus:");
        add(jLabel3);
        jLabel3.setBounds(10, 440, 710, 14);

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "SERVER Options", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel2.setLayout(null);

        jLabel1.setBackground(new java.awt.Color(0, 0, 0));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Client");
        jPanel2.add(jLabel1);
        jLabel1.setBounds(10, 70, 30, 20);

        connection.setText("/H/saprouter.hcc.in.tum.de/S/3297/H/G51as1");
        jPanel2.add(connection);
        connection.setBounds(10, 40, 410, 20);

        client.setText("902");
        jPanel2.add(client);
        client.setBounds(50, 70, 30, 20);

        jLabel2.setBackground(new java.awt.Color(0, 0, 0));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("JCO Connection Path");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(10, 20, 130, 14);

        system.setText("51");
        jPanel2.add(system);
        system.setBounds(190, 70, 30, 20);

        jLabel7.setBackground(new java.awt.Color(0, 0, 0));
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("System");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(150, 70, 40, 20);

        jLabel8.setBackground(new java.awt.Color(0, 0, 0));
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Language");
        jPanel2.add(jLabel8);
        jLabel8.setBounds(280, 70, 60, 20);

        language.setText("DE");
        jPanel2.add(language);
        language.setBounds(340, 70, 30, 20);

        add(jPanel2);
        jPanel2.setBounds(20, 20, 480, 110);

        jPanel4.setBackground(new java.awt.Color(0, 0, 0));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Username And Password Options", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel4.setLayout(null);

        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("User name");
        jPanel4.add(jLabel5);
        jLabel5.setBounds(10, 30, 60, 20);

        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Password");
        jPanel4.add(jLabel6);
        jLabel6.setBounds(10, 60, 60, 14);

        user.setText("jcows08-");
        jPanel4.add(user);
        user.setBounds(80, 30, 290, 20);
        jPanel4.add(jPasswordField1);
        jPasswordField1.setBounds(80, 60, 290, 20);

        add(jPanel4);
        jPanel4.setBounds(20, 140, 480, 100);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Connection Verification", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel1.setLayout(null);

        jButton5.setBackground(new java.awt.Color(0, 0, 0));
        jButton5.setText("Connect");
        jPanel1.add(jButton5);
        jButton5.setBounds(10, 30, 200, 30);

        jButton7.setBackground(new java.awt.Color(0, 0, 0));
        jButton7.setText("Disconnect");
        jPanel1.add(jButton7);
        jButton7.setBounds(280, 30, 160, 30);

        jButton8.setBackground(new java.awt.Color(0, 0, 0));
        jButton8.setText("Exit");
        jPanel1.add(jButton8);
        jButton8.setBounds(180, 70, 120, 30);

        add(jPanel1);
        jPanel1.setBounds(20, 260, 480, 120);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jButton1.setForeground(new java.awt.Color(255, 0, 51));
        jButton1.setText("TEST");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(570, 160, 150, 50);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        sap.Connection.testRun();
    }//GEN-LAST:event_jButton1ActionPerformed

  
   
    
    private void setPassword()
    {
      password="";
      char pass []=jPasswordField1.getPassword();
      for(int i=0;i<pass.length;i++)
      this.password+=pass[i];
    }
    
    private void setUsername()
    {
        this.user_name=user.getText();
    }
    
    private void reset_Username_and_Password()
    {
     this.user_name=null;
     this.password=null;
     user.setText("");
     jPasswordField1.setText("");
    }

    
    protected synchronized  void connect()
    { 
      this.setDefaultData();
      try{
			new sap.Connection(client.getText(), this.user_name, this.password, language.getText(), connection.getText(), system.getText());
            jProgressBar1.setIndeterminate(false);
            jLabel3.setText("Connected To SAP with JCo");
            jProgressBar1.setValue(100);
            setDefaultData();
            cm.jMenuItem1.setEnabled(true);
            JOptionPane.showMessageDialog(cm, "Connected, now you need to select a project" +
                    "on the menu bar to continue", "Connection", JOptionPane.INFORMATION_MESSAGE);
            if(cm.initiated)
            {
                cm.initiated = true;
            }
            else
            {
                cm.initTabs();
            }
         }
      catch (JCO.Exception ex)
      {
            JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
            jProgressBar1.setIndeterminate(false);
            connection.setEditable(true);
            user.setEditable(true);
            jPasswordField1.setEditable(true);
       }
      
      
      
    }
    
    protected void disconnect()    {      
      
           jProgressBar1.setIndeterminate(true);
           jLabel3.setText("Disconnecting...");
           try{
           jLabel3.setText("Disconnected");
           sap.Connection.getInstance().disconnect();
           }
           catch(Exception g){ 
                             
                            javax.swing.JOptionPane.showMessageDialog(this,"There Is No Connection In Use","Error",1);
                             
                             }
           
            cm.hideTabs();
            jProgressBar1.setIndeterminate(false);                           
            jProgressBar1.setIndeterminate(false);           
            connection.setEditable(true);
            user.setEditable(true);
            jPasswordField1.setEditable(true);         
            jProgressBar1.setValue(0);
            JOptionPane.showMessageDialog(cm, "Disconnected", "Connection", JOptionPane.INFORMATION_MESSAGE);
    }    
  
    
    protected void setDefaultData()
    { if(connection.getText().equals("") || user.getText().equals(""))
          javax.swing.JOptionPane.showMessageDialog(cm,"Username or JCO path is not valid","Error", 1);
      else{
     setPassword();
     setUsername();    
      }
      
    }
    
    
    protected synchronized String getJCoPath()
    {
        return this.JCo_path;
    }
    
    protected synchronized String getUserName()
    {
        return this.user_name;
    }
    
    protected synchronized String getPassword()
    {
        return this.password;
    }  
    
    
    public void actionPerformed(java.awt.event.ActionEvent e) {
        
       
        if(e.getSource().equals(jButton5))
        {
            this.connect();
            
        }
        else if(e.getSource().equals(jButton7))
        {
            this.disconnect();
        }
        else if(e.getSource().equals(jButton8))
        {
            System.exit(0);
        }
        
    }
    
    protected String JCo_path="/H/saprouter.hcc.in.tum.de/S/3297/H/G51as1";
    protected boolean autocommited=false;
    protected String user_name;
    protected String password;
    protected CManager cm;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField client;
    protected javax.swing.JTextField connection;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    protected javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    protected javax.swing.JPasswordField jPasswordField1;
    protected javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JTextField language;
    private javax.swing.JTextField system;
    protected javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables
    
}
